﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VfAccountApps.Domain.Models;

namespace VfAccountApps.Controllers
{ 
    public class SapController : Controller
    {
        private vfaEntities db = new vfaEntities();

        //
        // GET: /Sap/

        [Authorize]
        public ViewResult Index()
        {
            return View(db.sap_info.ToList());
        }

        //
        // GET: /Sap/Create

        [Authorize]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Sap/Create

        [Authorize]
        [HttpPost]
        public ActionResult Create(sap_info sap_info)
        {
            if (ModelState.IsValid)
            {
                var check = db.sap_info.Where(x => x.account_sap == sap_info.account_sap).ToList();

                if (check.Count == 0)
                {
                    db.sap_info.AddObject(sap_info);
                    db.SaveChanges();
                    return RedirectToAction("Create");
                }
                else
                {
                    ModelState.AddModelError("account_sap", "Already Exists");
                }
                 
            }

            return View(sap_info);
        }
        
        //
        // GET: /Sap/Edit/5

        [Authorize]
        public ActionResult Edit(int id)
        {
            sap_info sap_info = db.sap_info.Single(s => s.sap_info_id == id);
            return View(sap_info);
        }

        //
        // POST: /Sap/Edit/5

        [Authorize]
        [HttpPost]
        public ActionResult Edit(sap_info sap_info)
        {
            if (ModelState.IsValid)
            {
                var check = db.sap_info.Where(x => x.account_sap == sap_info.account_sap && x.sap_info_id != sap_info.sap_info_id).ToList();

                if (check.Count == 0)
                {
                    db.sap_info.Attach(sap_info);
                    db.ObjectStateManager.ChangeObjectState(sap_info, EntityState.Modified);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("account_sap", "Already Exists");
                }
            }
            return View(sap_info);
        }

        //
        // GET: /Sap/Delete/5

        [Authorize]
        public ActionResult Delete(int id)
        {
            var check = db.budget_info.Where(x => x.sap_info_id == id).ToList();

            if (check.Count == 0)
            {
                sap_info sap_info = db.sap_info.Single(s => s.sap_info_id == id);
                return View(sap_info);
            }

            ViewBag.message = "You Can't Delete this Entry";

            return View("Index", db.sap_info.ToList());
        }

        //
        // POST: /Sap/Delete/5

        [Authorize]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            sap_info sap_info = db.sap_info.Single(s => s.sap_info_id == id);
            db.sap_info.DeleteObject(sap_info);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}